package sdc.com.petscalendar;

import java.util.ArrayList;
import java.util.List;

import sdc.com.petscalendar.adapters.MFragmentPagerAdapter;
import sdc.com.petscalendar.components.MViewPager;
import sdc.com.petscalendar.fragments.BaseFragment;
import sdc.com.petscalendar.fragments.CalendarGroupFragment;
import sdc.com.petscalendar.fragments.PetListGroupFragment;
import sdc.com.petscalendar.fragments.ParasiteGroupFragment;
import sdc.com.petscalendar.fragments.TreatmentGroupFragment;
import sdc.com.petscalendar.fragments.VetoGroupFragment;
import sdc.com.petscalendar.utils.Statics;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
		View.OnClickListener, ViewPager.OnPageChangeListener {

	private TextView mTvTitle;
	private View mBtnMenu, mBtnNavLeft;

	private MViewPager mViewPager;
	private MFragmentPagerAdapter mPagerAdapter;
	private List<BaseFragment> mFragments;
	private SlidingPaneLayout mSlidingPaneLayout;

	private View mTabPetList, mTabCalendar, mTabParasite, mTabTreament,
			mTabVeto, mTabSettings;
	private View current = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initComponents();
		initPagers();
		addListener();

	}

	private void initComponents() {
		mViewPager = (MViewPager) findViewById(R.id.mViewPager);
		mBtnMenu = findViewById(R.id.btn_menu);
		mBtnNavLeft = findViewById(R.id.btn_nav_left);
		mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.sliding_std_en);

		mTabCalendar = findViewById(R.id.ll_calendar);
		mTabParasite = findViewById(R.id.ll_parasite);
		mTabPetList = findViewById(R.id.ll_pet_list);
		mTabSettings = findViewById(R.id.ll_settings);
		mTabTreament = findViewById(R.id.ll_treament);
		mTabVeto = findViewById(R.id.ll_veto);

		mTabPetList.setSelected(true);
		current = mTabPetList;
		mViewPager.setCurrentItem(0);
		
		mTvTitle = (TextView) findViewById(R.id.tv_title);
		mBtnNavLeft = findViewById(R.id.btn_nav_left);
		
	}

	private void addListener() {
		mTabCalendar.setOnClickListener(this);
		mTabParasite.setOnClickListener(this);
		mTabPetList.setOnClickListener(this);
		mTabSettings.setOnClickListener(this);
		mTabTreament.setOnClickListener(this);
		mTabVeto.setOnClickListener(this);

		mBtnMenu.setOnClickListener(this);

		mViewPager.setOnPageChangeListener(this);

	}

	private void initPagers() {
		mFragments = new ArrayList<BaseFragment>();

		mFragments.add(new PetListGroupFragment());
		mFragments.add(new CalendarGroupFragment());
		mFragments.add(new ParasiteGroupFragment());
		mFragments.add(new TreatmentGroupFragment());
		mFragments.add(new VetoGroupFragment());

		mPagerAdapter = new MFragmentPagerAdapter(getSupportFragmentManager(),
				mFragments);
		mViewPager.setPagingEnabled(false);
		mViewPager.setAdapter(mPagerAdapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_menu:
			mSlidingPaneLayout.openPane();
			break;
		case R.id.ll_pet_list:
			changeTab(0);
			break;
		case R.id.ll_calendar:
			changeTab(1);
			break;
		case R.id.ll_parasite:
			changeTab(2);
			break;
		case R.id.ll_treament:
			changeTab(3);
			break;
		case R.id.ll_veto:
			changeTab(4);
			break;

		default:
			break;
		}

	}

	private void changeTab(int position) {
		mSlidingPaneLayout.closePane();
		BaseFragment curFragment = mPagerAdapter.getItem(mViewPager
				.getCurrentItem());
		while(curFragment.getChildFragmentManager().getBackStackEntryCount() > 0) {
			curFragment.getChildFragmentManager().popBackStackImmediate();
		}
		mViewPager.setCurrentItem(position, false);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		switch (position) {
		case 0:
			if (current != null) {
				current.setSelected(false);
			}
			mTabPetList.setSelected(true);
			current = mTabPetList;
			break;
		case 1:
			if (current != null) {
				current.setSelected(false);
			}
			mTabCalendar.setSelected(true);
			current = mTabCalendar;
			setTile(getString(R.string.sb_tab_2));
			break;
		case 2:
			if (current != null) {
				current.setSelected(false);
			}
			setTile(getString(R.string.sb_tab_3));
			mTabParasite.setSelected(true);
			current = mTabParasite;
			break;
		case 3:
			if (current != null) {
				current.setSelected(false);
			}
			setTile(getString(R.string.sb_tab_4));
			mTabTreament.setSelected(true);
			current = mTabTreament;
			break;
		case 4:
			if (current != null) {
				current.setSelected(false);
			}
			setTile(getString(R.string.sb_tab_5));
			mTabVeto.setSelected(true);
			current = mTabVeto;
			break;
		/*
		 * case 5: if(current!=null){ current.setSelected(false); }
		 * mTabPetList.setSelected(true); current = mTabPetList; break;
		 */

		default:
			break;
		}
	}

	public void setTile(String title) {
		mTvTitle.setText(title);
	}

	public void setBtnNavLeftImageResource(int res) {
		((ImageView) mBtnNavLeft).setImageResource(res);
	}

	public void setBtnNavLeftOnClickListener(View.OnClickListener listener) {
		mBtnNavLeft.setOnClickListener(listener);
	}

	public void showBtnNavLeft(boolean isShow) {
		if (isShow) {
			mBtnNavLeft.setVisibility(View.VISIBLE);
		} else {
			mBtnNavLeft.setVisibility(View.INVISIBLE);
		}

	}

	@Override
	public void onBackPressed() {
		if (mSlidingPaneLayout.isOpen()) {
			mSlidingPaneLayout.closePane();
			return;
		}
		BaseFragment curFragment = mPagerAdapter.getItem(mViewPager
				.getCurrentItem());
		if (curFragment.getChildFragmentManager().getBackStackEntryCount() > 0) {
			curFragment.getChildFragmentManager().popBackStack();
			return;
		}
		super.onBackPressed();
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Statics.sData =data;
		Statics.sResultCode = resultCode;
		Statics.sRequestCode = requestCode;
		
		super.onActivityResult(requestCode, resultCode, data);
	}
}
